﻿<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <link rel="stylesheet" type="text/css" href="~/Scripts/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="~/Scripts/themes/icon.css">
    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
    <script src="~/Scripts/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="~/Scripts/wccc.js" type="text/javascript"></script>
    <script type="text/javascript">

        //要隐藏的行的索引集合.
        var hideIndexs = new Array();
        
        function endEdit() {
            var rows = $('#dg').datagrid('getRows');
            for (var i = 0; i < rows.length; i++) {
                $('#dg').datagrid('endEdit', i);
            }
        }

        function loadGrid(ObjectType) {

            var getUrl = "/" + ObjectType + "/GetList";
            var saveUrl = "/" + ObjectType + "/SaveList";
            var addUrl = "/" + ObjectType + "/Add";

            $('#ss').searchbox({
                width: 200,
                searcher: function (value) {
                    hideIndexs.length = 0;
                    if (value == '请输入查询内容') {
                        value = '';
                    }
                    //结束datagrid的编辑.
                    endEdit();
                    var rows = $('#dg').datagrid('getRows');

                    var cols = $('#dg').datagrid('options').columns[0];

                    for (var i = rows.length - 1; i >= 0; i--) {
                        var row = rows[i];
                        var isMatch = false;
                        for (var j = 0; j < cols.length; j++) {

                            var pValue = row[cols[j].field];
                            if (pValue == undefined) {
                                continue;
                            }
                            if (pValue.toString().indexOf(value) >= 0) {
                                isMatch = true;
                                break;
                            }

                        }
                        if (!isMatch)
                            hideIndexs.push(i);
                        $('#dg').datagrid('refreshRow', i);
                    }


                },
                prompt: '请输入查询内容'
            });

            $('#dg').datagrid({
                url: getUrl,
                loadMsg: '数据加载中请稍后……',
                rowStyler: function (index, row) {
                    if (hideIndexs.indexOf(index) >= 0) { return ' display:none'; }
                },
                toolbar: [{
                    text: "添加",
                    iconCls: "icon-add",
                    handler: function () {
                        $('#dg').datagrid('insertRow', { index: 0,    // index start with 0
                            row: addObject()
                        });

                        $('#dg').datagrid('beginEdit', 0);

                    }
                }, {
                    text: "编辑",
                    iconCls: "icon-edit",
                    handler: function () {
                        var rows = $('#dg').datagrid('getSelections');
                        if (rows.length > 0) {
                            for (var i = 0; i < rows.length; i++) {
                                var rowIndex = $('#dg').datagrid('getRowIndex', rows[i]);
                                $('#dg').datagrid('beginEdit', rowIndex);
                            }
                        }
                    }
                }, {
                    text: "删除",
                    iconCls: "icon-remove",
                    handler: function () {
                        var rows = $('#dg').datagrid('getSelections');
                        if (rows.length > 0) {
                            for (var i = rows.length-1; i >=0; i--) {
                                var rowIndex = $('#dg').datagrid('getRowIndex', rows[i]);
                                $('#dg').datagrid('deleteRow', rowIndex);
                            }
                        }
                    }
                }, {
                    text: "结束编辑",
                    iconCls: "icon-cancel",
                    handler: function () {
                        endEdit();
                    }
                }, {
                    text: "保存",
                    iconCls: "icon-save",
                    handler: function () {
                        endEdit();
                        if ($('#dg').datagrid('getChanges').length) {
                            var inserted = $('#dg').datagrid('getChanges', "inserted");
                            var deleted = $('#dg').datagrid('getChanges', "deleted");
                            var updated = $('#dg').datagrid('getChanges', "updated");

                            var effectRow = new Object();
                            if (inserted.length) {
                                effectRow["inserted"] = JSON.stringify(inserted);
                            }
                            if (deleted.length) {
                                effectRow["deleted"] = JSON.stringify(deleted);
                            }
                            if (updated.length) {
                                effectRow["updated"] = JSON.stringify(updated);
                            }

                            $.post(saveUrl, effectRow, function (rsp) {
                                if (rsp == "Success") {
                                    alert("提交成功！");
                                    $('#dg').datagrid('reload');
                                }
                            }, "JSON").error(function () {
                                alert("提交错误了！");
                            });
                        }
                    }
                }]
            });
//            $('#dg').datagrid({ onClickRow: function (rowIndex, rowData) {
//                $("#dg").datagrid('unselectAll');
//                $("#dg").datagrid('selectRow', rowIndex);
//            }
//            });
            $("#dg").datagrid('reload');
        }

      
    </script>
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>
